﻿/// -----------------------------------------------------------------
/// XmlConstructor.cs: Helper class to create and format Xml Documents
/// License: see http://mve.codeplex.com/license; All Rights follows the MS-PL
/// Current owner: shiniwa
/// The project decription: please refer to http://codeplex.com/mve/
/// -----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace MVEContentSvc
{
    /// <summary>
    /// Helper class to create and format Xml Documents 
    /// that can be sent as respons-strings to callers of the Service.
    /// Also use this class to Elements such as Clips, Segments and Keywords
    /// to the Xml Documents.
    /// </summary>
    public class XmlConstructor
    {
        /// <summary>
        /// Format an empty Xml Document as a response document.
        /// </summary>
        /// <remarks>INSERT:Creds</remarks>
        /// <param name="document">The document to format</param>
        /// <returns>The top element under which to add the response data.</returns>
        internal static XmlElement FormatResponseDocument(ref XmlDocument document)
        {
            document = new XmlDocument();
            XmlDeclaration dec = document.CreateXmlDeclaration(
                "1.0", "UTF-8", null);

            document.AppendChild(dec);

            XmlElement root = document.CreateElement("response");
            document.AppendChild(root);

            XmlElement status = document.CreateElement("status");
            status.InnerText = "0";
            root.AppendChild(status);

            return root;
        }//end:FormatResponseDocument

        /// <summary>
        /// Get an XmlDocument that says the operation specified failed...
        /// Not used at the moment.
        /// </summary>
        /// <remarks>INSERT:Creds</remarks>
        internal static XmlDocument GetOperationFailedResponseDocument()
        {
            XmlDocument document = new XmlDocument();
            XmlDeclaration dec = document.CreateXmlDeclaration(
                "1.0", "utf-8", null);

            document.AppendChild(dec);

            XmlElement root = document.CreateElement("response");
            document.AppendChild(root);

            XmlElement status = document.CreateElement("status");
            status.InnerText = "1";
            root.AppendChild(status);

            return document;
        }//end:GetOperationFailedResponseDocument

        /// <summary>
        /// Adds the data from a Clip to the given xml document
        /// </summary>
        /// <remarks>INSERT:Creds</remarks>
        /// <param name="document">Document to add the Clip to</param>
        /// <param name="clip">Clip to add</param>
        /// <param name="dataRootElement">Root of the clip element</param>
        /// <returns>The newly added clip element.</returns>
        internal static XmlElement AddClipNodeToDocument(XmlDocument document, ClipOperations.Clip clip, XmlElement dataRootElement)
        {
            XmlElement clipRoot = document.CreateElement("clip");
            dataRootElement.AppendChild(clipRoot);

            XmlElement idE = document.CreateElement("id");
            idE.InnerText = clip.Id.ToString();
            clipRoot.AppendChild(idE);

            XmlElement titleE = document.CreateElement("title");
            titleE.InnerText = clip.Title;
            clipRoot.AppendChild(titleE);

            XmlElement authorE = document.CreateElement("author");
            authorE.InnerText = clip.Author;
            clipRoot.AppendChild(authorE);

            XmlElement totaltimeE = document.CreateElement("totaltime");
            totaltimeE.InnerText = clip.TotalTime.ToString();
            clipRoot.AppendChild(totaltimeE);

            XmlElement urlE = document.CreateElement("url");
            urlE.InnerText = clip.Url;
            clipRoot.AppendChild(urlE);

            XmlElement onair_dateE = document.CreateElement("onair_date");
            onair_dateE.InnerText = clip.OnAirDate.ToString("yyyy'-'MM'-'dd HH':'mm':'ss");
            clipRoot.AppendChild(onair_dateE);

            return clipRoot;
        }//end:AddClipNodeToDocument

        /// <summary>
        /// Adds the data from a segment to the given clip node of the given document.
        /// </summary>
        /// <remarks>INSERT:Creds</remarks>
        /// <param name="document">Parent document</param>
        /// <param name="segment">Segment to add</param>
        /// <param name="clipOnAirDate">On Air Date of parent Clip</param>
        /// <param name="dataRootElement">Root of the Segment element</param>
        /// <returns>The newly added segment element.</returns>
        internal static XmlElement AddSegmentNodeToClip(XmlDocument document, ClipOperations.Segment segment, DateTime clipOnAirDate, XmlElement dataRootElement)
        {
            XmlElement segmentRoot = document.CreateElement("segment");
            dataRootElement.AppendChild(segmentRoot);

            XmlElement idE = document.CreateElement("id");
            idE.InnerText = segment.Id.ToString();
            segmentRoot.AppendChild(idE);

            XmlElement clipidE = document.CreateElement("clip_id");
            clipidE.InnerText = segment.ParentClipId.ToString();
            segmentRoot.AppendChild(clipidE);

            XmlElement titleE = document.CreateElement("title");
            titleE.InnerText = segment.Title;
            segmentRoot.AppendChild(titleE);

            XmlElement descriptionE = document.CreateElement("description");
            descriptionE.InnerText = segment.Description;
            segmentRoot.AppendChild(descriptionE);

            XmlElement onair_dateE = document.CreateElement("onair_date");
            onair_dateE.InnerText = clipOnAirDate.ToString("yyyy'-'MM'-'dd HH':'mm':'ss");
            segmentRoot.AppendChild(onair_dateE);

            XmlElement start_timeE = document.CreateElement("start_time");
            start_timeE.InnerText = segment.StartSecond.ToString();
            segmentRoot.AppendChild(start_timeE);

            XmlElement end_timeE = document.CreateElement("end_time");
            end_timeE.InnerText = segment.EndSecond.ToString();
            segmentRoot.AppendChild(end_timeE);

            XmlElement thumb_urlE = document.CreateElement("thumb_url");
            thumb_urlE.InnerText = segment.ThumbUrl;
            segmentRoot.AppendChild(thumb_urlE);

            return segmentRoot;
        }//end:AddSegmentNodeToClip

        /// <summary>
        /// Adds the data from a keyword to the given segment node of the given document.
        /// </summary>
        /// <remarks>INSERT:Creds</remarks>
        /// <param name="document">Parent document</param>
        /// <param name="keyword">Keyword to add</param>
        /// <param name="dataRootElement">Root of the Keyword element</param>
        /// <returns>The newly added Keyword element.</returns>
        internal static XmlElement AddKeywordNodeToSegment(XmlDocument document, KeywordOperations.Keyword keyword, XmlElement dataRootElement)
        {
            XmlElement keywordRoot = document.CreateElement("keyword");
            dataRootElement.AppendChild(keywordRoot);

            XmlElement idE = document.CreateElement("id");
            idE.InnerText = keyword.id.ToString();
            keywordRoot.AppendChild(idE);

            XmlElement seg_idE = document.CreateElement("segment_id");
            seg_idE.InnerText = keyword.SegmentId.ToString();
            keywordRoot.AppendChild(seg_idE);

            XmlElement wordE = document.CreateElement("keyword");
            wordE.InnerText = keyword.KeywordString;
            keywordRoot.AppendChild(wordE);

            return keywordRoot;

        }//end:AddKeywordNodeToSegment
    }
}
